package com.xxwy.configsimple.config;

import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.*;

import java.util.HashMap;

/**
 * @author xxwy
 * on 2018/9/28 0028
 */
@Configuration
public class MyPropertySourceLocator implements PropertySourceLocator{

    @Override
    public PropertySource<?> locate(Environment environment) {
        if(environment instanceof ConfigurableEnvironment){

            ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) environment;
            //获取PropertySources
            MutablePropertySources propertySources = configurableEnvironment.getPropertySources();
            //定义一个新的PropertySource,放在首位
            propertySources.addFirst(createPropertySource());
        }

        return null;
    }

    private PropertySource createPropertySource() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("spring.application.name","加载顺序");
        MapPropertySource mapPropertySource = new MapPropertySource("my-property-source2",map);
        return mapPropertySource;
    }

}
